package com.ruoyi.fangyuan.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.fangyuan.config.EncryptorBean;
import com.ruoyi.fangyuan.config.Master;
import com.ruoyi.fangyuan.config.Slave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.fangyuan.domain.TbRoomInfo;
import com.ruoyi.fangyuan.service.ITbRoomInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 房源列表Controller
 * 
 * @author ruoyi
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/info")
public class TbRoomInfoController extends BaseController
{
    @Autowired
    private ITbRoomInfoService tbRoomInfoService;


/*    @PostConstruct
    public void es(){
        List<TbRoomInfo> roomInfos = tbRoomInfoService.getList();
        List<IndexQuery> list = new ArrayList<>();
        for (TbRoomInfo roomInfo : roomInfos) {
            IndexQuery build = new IndexQueryBuilder()
                    .withObject(roomInfo)
                    .withId(roomInfo.getId() + "")
                    .build();
            list.add(build);
        }
        restTemplate.bulkIndex(list,TbRoomInfo.class);
    }*/

    /**
     * 查询房源列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbRoomInfo tbRoomInfo)
    {
        startPage();
        List<TbRoomInfo> list = tbRoomInfoService.selectTbRoomInfoList(tbRoomInfo);
        return getDataTable(list);
    }

    @Log(title = "房源列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRoomInfo tbRoomInfo)
    {
        List<TbRoomInfo> list = tbRoomInfoService.selectTbRoomInfoList(tbRoomInfo);
        ExcelUtil<TbRoomInfo> util = new ExcelUtil<TbRoomInfo>(TbRoomInfo.class);
        util.exportExcel(response, list, "房源列表数据");
    }

    /**
     * 获取房源列表详细信息
     */
//    @RequiresPermissions("fangyuan:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {

        return success(tbRoomInfoService.selectTbRoomInfoById(id));
    }

    /**
     * 新增房源列表
     */
//    @RequiresPermissions("fangyuan:info:add")
    @Log(title = "房源列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRoomInfo tbRoomInfo)
    {
        tbRoomInfo.setId(UUID.randomUUID().toString());
        tbRoomInfo.setHouseId(UUID.randomUUID().toString());
        return toAjax(tbRoomInfoService.insertTbRoomInfo(tbRoomInfo));
    }

    /**
     * 修改房源列表
     */
//    @RequiresPermissions("fangyuan:info:edit")
    @Log(title = "房源列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRoomInfo tbRoomInfo)
    {
        return toAjax(tbRoomInfoService.updateTbRoomInfo(tbRoomInfo));
    }

    /**
     * 删除房源列表
     */
//    @RequiresPermissions("fangyuan:info:remove")
    @Log(title = "房源列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbRoomInfoService.deleteTbRoomInfoByIds(ids));
    }

    /**
     * 生成配置信息加密
     */
    @Autowired
    EncryptorBean encryptorBean;
    @GetMapping("getMySecretValue")
    public void generateEncryptedValue() {
        String encryptedValue = encryptorBean.encrypt("root");
        System.out.println("root value: " + encryptedValue);
        String encryptedValue1 = encryptorBean.encrypt("123456");
        System.out.println("123456 value: " + encryptedValue1);
    }
}
